﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using NotAClue.Web;
using NotAClue;

namespace DD_Extensions
{
    public partial class SprocParameterValue_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.MaxLength = Column.MaxLength;
            if (Column.MaxLength < 20)
                TextBox1.Columns = Column.MaxLength;
            TextBox1.ToolTip = Column.Description;

            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(RegularExpressionValidator1);
            SetUpValidator(DynamicValidator1);

            if (DropDownList1.Items.Count == 0)
            {
                if (Mode == DataBoundControlMode.Insert || !Column.IsRequired)
                    DropDownList1.Items.Add(new ListItem("[Not Set]", String.Empty));

                EnumWebExtensionMethods.FillEnumListControl(DropDownList1, typeof(DateTimeTypes));
            }
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            var sprocParameter = Row.GetEntityAs<Models.SprocParameter>();
            var dataType = sprocParameter.DataType.AsEnum<SqlDbType>();
            if (dataType == SqlDbType.Date || dataType == SqlDbType.DateTime || dataType == SqlDbType.DateTime2 || dataType == SqlDbType.SmallDateTime)
            {
                if (!String.IsNullOrEmpty(FieldValueString))
                {
                    var parts = FieldValueString.ToArray(seperator: '|');
                    if (parts.Length > 0)
                    {
                        var dateType = parts[0].ToEnum<DateTimeTypes>();

                        ListItem item = DropDownList1.Items.FindByText(dateType.ToString());
                        if (item != null)
                            DropDownList1.SelectedValue = item.Value;

                        if (parts.Length > 1)
                            TextBox1.Text = parts[1];
                        else
                            TextBox1.Text = "0";
                    }
                    else
                        TextBox1.Text = "0";
                }

                PlaceHolder2.Visible = true;
            }
            else
                TextBox1.Text = FieldValueString;
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            var value = String.Empty;
            if (DropDownList1.Visible)
                value = String.Format("{0}|{1}", DropDownList1.SelectedItem.Text, TextBox1.Text);
            else
                value = TextBox1.Text;

            dictionary[Column.Name] = value;
        }

        public override Control DataControl
        {
            get
            {
                return TextBox1;
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
